package innovacion.servlet.idea;

import innovacion.excepcion.DAOExcepcion;
import innovacion.modelo.Idea;
import innovacion.negocio.GestionIdea;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class IdeaBuscar
 */
@WebServlet("/IdeaBuscarServlet")
public class IdeaBuscarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdeaBuscarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//String btnenviar = request.getParameter("btnenviar");
		//System.out.println(btnenviar);
		
		//if(btnenviar.equalsIgnoreCase("buscar")){
			
			
			String titulo = request.getParameter("titulo");
			String estado = request.getParameter("estado");
			System.out.println(estado);
			GestionIdea negocio = new GestionIdea();
			try {
				
				if(estado.equalsIgnoreCase("creada")){
					Collection<Idea> lista = negocio.listar_estado(estado, titulo);
					if(lista.size()>0){
						request.setAttribute("listar_estado", lista);
						request.setAttribute("opcion","1");
					}else{
						request.setAttribute("mensaje", "2");
					}
				}else{
					if(estado.equalsIgnoreCase("publicada")){
						Collection<Idea> lista = negocio.listar_estado(estado, titulo);
						if(lista.size()>0){
							request.setAttribute("listar_estado", lista);
							request.setAttribute("opcion","2");
						}else{
							request.setAttribute("mensaje", "4");
						}
					}
				}					
				
			
				
				
				RequestDispatcher rd = request.getRequestDispatcher("publicar_ideas.jsp");
				rd.forward(request, response);

			} catch (DAOExcepcion e) {
				System.out.println(e.getMessage());
				RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
				rd.forward(request, response);
			}
		//}else{
			/*if(btnenviar.equalsIgnoreCase("publicar")){
			String [] ideas= request.getParameterValues("checkidea");
			if (ideas!= null && ideas.length>0) {
			    for (int i = 0; i < ideas.length; i++) {
				        System.out.println(ideas[i]);
			    }
			  } else System.out.println ("none selected");
		}
		}*/
			}
		
		
	}


